<?php

/**
+----------------------------------------------------------------------
| 通知、推送相关
+----------------------------------------------------------------------
*/
//发送通知
function ext_add_notice($keytype,$keyid,$content,$client_id,$from_id=1,$push_method=1){
    $sql_helper = new Mysql();

    //写入到数据库
    _ext_notice_add_table($sql_helper, $keytype,$keyid,$content,$client_id,$from_id,$push_method);
    //发送推送
    _ext_notice_push($sql_helper,$keytype,$keyid,$content,$client_id,$push_method);

    sys_close_db($sql_helper);
}
//聊天推送
function ext_msg_add_notice($keytype,$keyid,$content,$client_id,$from_id=1,$push_method=1){
    $sql_helper = new Mysql();
    //发送推送
    _ext_notice_push($sql_helper,$keytype,$keyid,$content,$client_id,$push_method);
    sys_close_db($sql_helper);
}
//发送系统通知时，将通知写入到数据库
function _ext_notice_add_table($sql_helper,$keytype,$keyid,$content,$client_id,$from_id,$push_method){
    $regdate = sys_get_time();

    //整合通知类型
    $notice_type = 1;
    //插入系统通知表
    if($push_method == 1){
        //指定用户
        $sqlstr = "insert into sys_notice(keytype,keyid,content,client_id,from_id,looktype,regdate) values ";
        $client_id_a = explode(',',$client_id);
        $sql_values = "";
        foreach ($client_id_a as $client_id_a_i){
            $sql_values .= "($keytype,$keyid,'$content',$client_id_a_i,$from_id,1,'$regdate'),";
        }
        if($client_id_a){
            $sqlstr .= substr($sql_values,0,-1);
            $sql_helper -> do_execute($sqlstr);
        }
    }
    else if($push_method == 2){
        //放入消息队列中处理
        $param = ['action'=>'all_notice_add_user','param'=>[
            'keytype'=>$keytype,
            'keyid'=>$keyid,
            'push_content'=>$content,
            'from_id'=>$from_id
        ]];
        Queue::enqueue('Notice',$param);
    }
}
//调用第三方进行通知
function _ext_notice_push($sql_helper,$keytype,$keyid,$content,$client_id,$push_method){
    if($push_method == 1){//向用户推
        $sqlstr="select channelid,devicetype from sys_client where id in($client_id) and channelid <>'' ";
        $client_array= $sql_helper -> get_list_bysql($sqlstr);
        if(count($client_array) > 1){//推送到指定多用户
            $cid_list = '';
            foreach ($client_array as $client_array_a){
                $cid_list .= $client_array_a['channelid'].',';
            }
            if($cid_list){
                $cid_list_1 = $cid_list ? substr($cid_list,0,-1) : '';
                @sys_list_push($cid_list,$content,$keytype,$keyid,1);
            }
        }
        else if(count($client_array) == 1){//推送到单用户
            if($client_array){
                @sys_push($client_array[0]['devicetype'],$client_array[0]['channelid'],$content,$keytype,$keyid);
            }
        }
    }
    else{//向app推
        @sys_app_push($content,$keytype,$keyid);
    }
}

/**
+----------------------------------------------------------------------
| 业务相关函数
+----------------------------------------------------------------------
*/
//账户余额变更记录

//curl访问其他
function ext_curl_post($url,$post_a)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_a);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//1,防止向客户端输出
    curl_setopt($ch, CURLOPT_NOSIGNAL, true);
    curl_setopt($ch, CURLOPT_TIMEOUT_MS, 10);
    $result = curl_exec($ch);//返回json字符串
    //echo $result;
    curl_close($ch);

    return  $result;
}


//发送手机短信(统一调用河马短信网关)
function ext_send_sms($mobile,$content){
    //
	if(empty($mobile) || empty($content)) return 0;	
	if(mb_strlen($content)>65) return 0;	// 短信内容最长65个字	
	$content=$content."(".SYS_ZH_NAME.")";
	$url ="http://oa.hemaapp.cn/index.php/Webadmin/?m=Manage&a=sms_send";
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_POSTFIELDS, "hema_pid=".HEMA_PID."&hema_pwd=".HEMA_PWD."&mobile=$mobile&content={$content}");
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//正式部署必须为1，防止向客户端输出
	//特别注意：调试时，将CURLOPT_RETURNTRANSFER置为0，查看河马短信网关返回的结果
	$result=curl_exec($ch);//河马网关返回一个整数 0:失败 1：成功 2：欠费
	curl_close($ch);		
	if($result==0) sys_out_fail(sys_get_msg(600),600);//网络故障
	if($result==2) sys_out_fail(sys_get_msg(601),601);//余额不足
} 
//查询手机短信剩余条数
function ext_smscount_get(){
	$url ="http://oa.hemaapp.cn/index.php/Webadmin/?m=Manage&a=smscount_get";
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_POSTFIELDS, "hema_pid=".HEMA_PID."&hema_pwd=".HEMA_PWD);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//正式部署必须为1，防止向客户端输出
	//特别注意：调试时，将CURLOPT_RETURNTRANSFER置为0，查看河马短信网关返回的结果
	$result=curl_exec($ch);//河马网关返回一个整数 0:失败 1：成功 2：欠费	
	curl_close($ch);	
	return $result;	
}

/**
+----------------------------------------------------------------------
| 通用函数
+----------------------------------------------------------------------
*/
//定义array_column函数
if(!function_exists('array_column')){
    function array_column(&$array,$column){
        $temp_array = array();
        foreach ($array as $array_i){
            $temp_array[] = $array_i[$column];
        }
        return $temp_array;
    }
}

//生成基本密码
if(!function_exists('base_password_create')){
    function base_password_create($password,$data_key=''){
        $data_key = $data_key ? $data_key : DATAKEY;
        return md5($data_key.md5($password));
    }
}

/**
+----------------------------------------------------------------------
| 发红包相关 start
+----------------------------------------------------------------------
 */

/**
 * 求一个数的平方
 * @param $n
 */
function sqr($n){
    return $n*$n;
}

/**
 * 生产min和max之间的随机数，但是概率不是平均的，从min到max方向概率逐渐加大。
 * 先平方，然后产生一个平方值范围内的随机数，再开方，这样就产生了一种“膨胀”再“收缩”的效果。
 */
function xRandom($bonus_min,$bonus_max){
    $sqr = intval(sqr($bonus_max-$bonus_min));
    $rand_num = rand(0, ($sqr-1));
    return intval(sqrt($rand_num));
}

